//#preprocess
package com.oc.claimobs.bb.screens;

import net.rim.blackberry.api.phone.Phone;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import ar.com.lemil.bb.http.detection.BlackberryConnections;

import com.oc.claimobs.bb.ScreenFlow;
import com.oc.claimobs.bb.facade.InfoService;
import com.oc.claimobs.bb.model.Info;
import com.oc.claimobs.bb.ui.TouchScreenFieldFocusChangeListener;
import com.oc.utils.Style;
import com.oc.utils.TextUtils;

public class InfoScreen extends MainScreen {
	private Bitmap backgroundBitmap;

	public InfoScreen() {
		super( MainScreen.NO_HORIZONTAL_SCROLL | MainScreen.NO_HORIZONTAL_SCROLLBAR);
		
		init();
	}

	BasicEditField fld_nombre;
	BasicEditField fld_apellido;
	ObjectChoiceField fld_tipodoc;
	BasicEditField fld_numero;
	BasicEditField fld_telefono;
	BasicEditField fld_email;
	BasicEditField fld_url;
	ObjectChoiceField fld_carrier;
	
	private void init() {

	
			
		// The background image.
		backgroundBitmap = Bitmap.getBitmapResource("img/title_back.png");

		VerticalFieldManager vfm = new VerticalFieldManager(
				VerticalFieldManager.NO_VERTICAL_SCROLL
						| VerticalFieldManager.NO_VERTICAL_SCROLLBAR
						| VerticalFieldManager.USE_ALL_WIDTH
						| VerticalFieldManager.NON_FOCUSABLE
						| VerticalFieldManager.USE_ALL_HEIGHT) {

			// Override the paint method to draw the background image.
			public void paint(Graphics graphics) {
				// Draw the background image and then call paint.
				graphics.drawBitmap(0, 0, Style.getScreenWidht(), 40, backgroundBitmap, 0, 0);
				super.paint(graphics);
			}
		};

		//Boton
		ButtonField button = new ButtonField("Aceptar", ButtonField.FOCUSABLE
						| ButtonField.RIGHT);
		button.setMargin(30,15,15,15);
		//button.setFocusListener(new TouchScreenButtonFocusChangeListener(this));
		button.setChangeListener(new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				//Validation
				reset();
				if(validate()){
					
					//Init data
					InfoService service = new InfoService();
					Integer k = new Integer(1);
					Info o = service.getByKey(k);
					if(o == null){
						o = new Info();
					}
					
					o.setNombre(fld_nombre.getText());
					o.setApellido(fld_apellido.getText());
					o.setLastClaimId(System.currentTimeMillis() + "");
					o.setDocumento(fld_numero.getText());
					o.setTelefono(fld_telefono.getText());
					o.setUrl(fld_url.getText());
					o.setCarrier(fld_carrier.toString());
					o.setEmail(fld_email.getText());
					
					String c = fld_tipodoc.toString() + "|" + fld_tipodoc.getSelectedIndex();
					o.setTipodoc(c);
					// Always the same.
					service.saveOrUpdate(k, o);
					//
					Screen screen = ScreenFlow.getInstance().getNext("");
					try {
						UiApplication.getUiApplication().pushScreen(screen);
						UiApplication.getUiApplication().popScreen(
								ScreenFlow.getInstance().getSETUP_PAGE());
					} catch (Exception e) {
						
					}
				}
			}
		});

		//Nombre
		LabelField lbl_nombre = new LabelField("Nombre*",Field.FIELD_LEFT | Field.NON_FOCUSABLE);
		lbl_nombre.setMargin(60, 12, 0, 12);
		vfm.add(lbl_nombre);

		fld_nombre = new BasicEditField("", "", 20, BasicEditField.NO_NEWLINE
				| BasicEditField.FILTER_DEFAULT | Field.NON_SPELLCHECKABLE);
		fld_nombre.setMargin(10, 18, 0, 15);
		fld_nombre.setFocusListener(new TouchScreenFieldFocusChangeListener(this));
		fld_nombre.setBorder(Style.fieldBorder());
		vfm.add(fld_nombre);
		
		//Apellido
		LabelField lbl_apellido = new LabelField("Apellido*",Field.FIELD_LEFT | Field.NON_FOCUSABLE);
		lbl_apellido.setMargin(20, 12, 0, 12);
		vfm.add(lbl_apellido);
		
		fld_apellido = new BasicEditField("", "", 20, BasicEditField.NO_NEWLINE | Field.NON_SPELLCHECKABLE
				| BasicEditField.FILTER_DEFAULT);
		fld_apellido.setMargin(10, 18, 0, 15);
		fld_apellido.setFocusListener(new TouchScreenFieldFocusChangeListener(this));
		fld_apellido.setBorder(Style.fieldBorder());
		vfm.add(fld_apellido);
		
		//Documento
		LabelField lbl_documento = new LabelField("Tipo y nro de documento*",Field.FIELD_LEFT | Field.NON_FOCUSABLE);
		lbl_documento.setMargin(20, 12, 0, 12);
		vfm.add(lbl_documento);
		
		HorizontalFieldManager hfm = new HorizontalFieldManager(
				HorizontalFieldManager.NO_HORIZONTAL_SCROLL
						| HorizontalFieldManager.NO_HORIZONTAL_SCROLLBAR
						| HorizontalFieldManager.FIELD_LEFT);

		String tipos[] = { "DNI", "CI", "LE", "LC" };
		fld_tipodoc = new ObjectChoiceField("", tipos, 1, Field.FIELD_LEFT);
		fld_tipodoc.setMargin(0, 10, 0, 10);
		fld_tipodoc.setSelectedIndex(0);
		//fld_tipodoc.setBorder(Style.fieldBorder());
		hfm.add(fld_tipodoc);

		fld_numero = new BasicEditField("", "", 9,
				BasicEditField.FILTER_NUMERIC | Field.FIELD_LEFT);
		//fld_numero.setBackground(BackgroundFactory.(0x00c0c0c0));
		fld_numero.setMargin(5, 18, 0, 18);
		fld_numero.setFocusListener(new TouchScreenFieldFocusChangeListener(this));
		fld_numero.setBorder(Style.fieldBorder());
		hfm.add(fld_numero);

		hfm.setMargin(20, 0, 0, 0);
		vfm.add(hfm);

		//Email
		LabelField lbl_email = new LabelField("Email",Field.FIELD_LEFT | Field.NON_FOCUSABLE);
		lbl_email.setMargin(10, 12, 0, 12);
		vfm.add(lbl_email);
		
		fld_email = new BasicEditField("", "", 255, BasicEditField.FILTER_EMAIL);
		fld_email.setMargin(10, 18, 0, 15);
		fld_email.setFocusListener(new TouchScreenFieldFocusChangeListener(this));
		fld_email.setBorder(Style.fieldBorder());
		vfm.add(fld_email);


		//Telefono
		LabelField lbl_telefono = new LabelField("Telefono",Field.FIELD_LEFT | Field.NON_FOCUSABLE);
		lbl_telefono.setMargin(10, 12, 0, 12);
		vfm.add(lbl_telefono);
		
		fld_telefono = new BasicEditField("", Phone.getDevicePhoneNumber(true), 15,
				BasicEditField.FILTER_PHONE) ;
		fld_telefono.setMargin(10, 18, 0, 15);
		fld_telefono.setFocusListener(new TouchScreenFieldFocusChangeListener(this));
		fld_telefono.setBorder(Style.fieldBorder());
		vfm.add(fld_telefono);

		
		//
		LabelField lbl_req = new LabelField("(*) Campos requerido",Field.FIELD_LEFT | Field.NON_FOCUSABLE);
		lbl_req.setFont(Style.h3());
		lbl_req.setMargin(20, 12, 0, 12);
		vfm.add(lbl_req);
		
		
		
		
		//URL
		LabelField lbl_url = new LabelField("Servicio Web",Field.FIELD_LEFT | Field.NON_FOCUSABLE);
		lbl_url.setMargin(20, 12, 0, 12);
		
		
		fld_url = new BasicEditField("", "", 255, BasicEditField.FILTER_URL);
		fld_url.setMargin(10, 18, 0, 15);
		fld_url.setBorder(Style.fieldBorder());
		
		
		//CARRIER
		LabelField lbl_carrier = new LabelField("Carrier",Field.FIELD_LEFT | Field.NON_FOCUSABLE);
		lbl_carrier.setMargin(20, 12, 0, 12);
		vfm.add(lbl_carrier);
		
		String carriers[] = BlackberryConnections.HTTP_WAP_CARRIERS;
		fld_carrier = new ObjectChoiceField("", carriers, 1, Field.FIELD_LEFT);
		fld_carrier.setMargin(0, 10, 0, 10);
		fld_carrier.setSelectedIndex(0);
		vfm.add(fld_carrier);

		vfm.add(button);

		//#ifndef IS_RELEASE
		vfm.add(lbl_url);
		vfm.add(fld_url);
		//#endif
		
		// Add the manager to the screen.
		add(vfm);
		
		try {
			//Init data
			InfoService service = new InfoService();
			Integer k = new Integer(1);
			Info o = service.getByKey(k);
			if(o == null){
				o = new Info();
				// Always the same.
				service.saveOrUpdate(k, o);
			}
	
			fld_nombre.setText(o.getNombre());
			fld_apellido.setText(o.getApellido());
			fld_numero.setText(o.getDocumento());
			
			try {
				String[] c = TextUtils.split(o.getTipodoc(), "|");
				int index = 0;
				if(c.length > 0){
					index = Integer.parseInt(c[1]);
				}
				fld_tipodoc.setSelectedIndex(index);
			} catch(Exception e){
				fld_tipodoc.setSelectedIndex(0);
			}
			
			fld_telefono.setText(o.getTelefono());
			
			if(o.getUrl().equalsIgnoreCase("")){
				fld_url.setText("http://cippec.ath.cx/reclamos/services/xml/reclamos.php");
			} else {
				fld_url.setText(o.getUrl());
			}
			
			fld_carrier.setSelectedIndex(o.getCarrier());

			fld_email.setText(o.getEmail());
		
		} catch(Exception ex){
			//
		}
	}
	
	public void reset()
	{
		fld_nombre.setBorder(Style.fieldBorder());
		fld_apellido.setBorder(Style.fieldBorder());
		fld_numero.setBorder(Style.fieldBorder());
		fld_email.setBorder(Style.fieldBorder());
	}
	
	public boolean validate()
	{
		boolean b = true;
		if(fld_nombre.getText().length() < 1){
			fld_nombre.setBorder(Style.errorFieldBorder());
			b = false;
		}
		
		if(fld_apellido.getText().length() < 1){
			fld_apellido.setBorder(Style.errorFieldBorder());
			b = false;
		}
		
		if(fld_numero.getText().length() < 1){
			fld_numero.setBorder(Style.errorFieldBorder());
			b = false;
		}

		if(!fld_email.isDataValid()){
			fld_email.setBorder(Style.errorFieldBorder());
			b = false;
		}
		
		if(!b){
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					Dialog.alert("Corrija los errores marcados en el formulario, e intente nuevamente");
				}
			});
		}
		return b;
	}
	
	protected boolean invokeAction(int action) {
		boolean handled = super.invokeAction(action);

		if (!handled) {
			switch (action) {
				case ACTION_INVOKE: // Trackball click.
				{
					return true;
				}
			}
		}
		return handled;
	}

}
